package org.yiming.tools.mqtt.client.command;

import lombok.RequiredArgsConstructor;
import org.yiming.tools.mqtt.client.ApplicationContext;

import java.util.stream.Collectors;

/**
 * @author renwenpeng
 * date  2022/3/25 5:14 下午
 */
@RequiredArgsConstructor
public class HelpCommand implements Command {

    private final String[] names = new String[]{"help"};

    private final ApplicationContext applicationContext;

    @Override
    public String[] name() {
        return names;
    }

    @Override
    public String execute(String[] args) {
        System.out.println("----------------------------- help start -----------------------------");
        System.out.println("quit process, please input \"q\" or \"fq\"");
        System.out.println("-----------------------------    main    -----------------------------");
        System.out.println(applicationContext.getCommandMap().values().stream()
                .filter(command -> !command.equals(this))
                .map(Command::helpContent)
                .map(strings -> strings.length < 2 ? strings[0] : (strings.length < 3
                        ? makeStringFixedLength(strings[0], 25) + "" + strings[1] : "")).collect(Collectors.joining("\n\r")));
        System.out.println("----------------------------- help end   -----------------------------");
        return null;
    }

    @Override
    public String[] helpContent() {
        return new String[]{"help"};
    }

    private static String makeStringFixedLength(String originString, int length) {
        int count = length - originString.length();
        while (count > 0) {
            originString += " ";
            count--;
        }
        return originString;
    }
}
